package com.it2297.view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Point;

import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JSpinner;

import com.it2297.controller.ProductController;
import com.it2297.controller.WorkshopController;
import com.it2297.model.Trainer;
import com.it2297.model.User;
import com.it2297.model.Workshop;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SpinnerDateModel;
import java.util.Date;
import java.util.Calendar;

public class CreateWorkshopView extends JPanel {
	private final WorkshopController c=new WorkshopController(this);
	private final JTextField workshopIdTxt=new JTextField();
	private final JTextField titleTxt=new JTextField();
	private final JTextField locationTxt=new JTextField();
	private final JTextArea textDescription=new JTextArea();
	private final User user=new User();
	
	private final JSpinner pplcapacity=new JSpinner();
	private final JSpinner availableslot=new JSpinner();
	private final JComboBox  status=new JComboBox();
	private final JLabel workshopimagelbl=new JLabel("");
	private JTextField dstxt;
	private JTextField detxt;
	private JComboBox trainerBox=new JComboBox();
	/**
	 * Create the panel.
	 */
	
	
	public CreateWorkshopView() {
		setLayout(null);
		
		JLabel lblCreateWorkshop = new JLabel("Create Workshop");
		lblCreateWorkshop.setFont(new Font("Tahoma", Font.ITALIC, 18));
		lblCreateWorkshop.setBounds(29, 11, 198, 31);
		add(lblCreateWorkshop);
		
		JLabel lblWorkshopId = new JLabel("Workshop ID:");
		lblWorkshopId.setBounds(29, 66, 106, 14);
		add(lblWorkshopId);
		
		JLabel lblNewLabel = new JLabel("Title:");
		lblNewLabel.setBounds(29, 102, 72, 14);
		add(lblNewLabel);
		
		JLabel lblDateStart = new JLabel("Date Start:");
		lblDateStart.setBounds(29, 150, 106, 14);
		add(lblDateStart);
		
		JLabel lblDateEnd = new JLabel("Date End:\r\n");
		lblDateEnd.setBounds(29, 204, 109, 14);
		add(lblDateEnd);
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setBounds(29, 243, 109, 14);
		add(lblDescription);
		
		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setBounds(29, 311, 86, 14);
		add(lblLocation);
		
		JLabel lblCapacity = new JLabel("Capacity:");
		lblCapacity.setBounds(29, 353, 86, 14);
		add(lblCapacity);
		
		JLabel lblNewLabel_1 = new JLabel("Available Slot:");
		lblNewLabel_1.setBounds(29, 390, 106, 14);
		add(lblNewLabel_1);
		
		
		workshopIdTxt.setBounds(180, 63, 283, 20);
		add(workshopIdTxt);
		workshopIdTxt.setColumns(10);
		
		
		titleTxt.setBounds(180, 99, 283, 20);
		add(titleTxt);
		titleTxt.setColumns(10);
		
		
		locationTxt.setBounds(180, 308, 283, 20);
		add(locationTxt);
		locationTxt.setColumns(10);
		
		
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(workshopIdTxt.getText().isEmpty()||titleTxt.getText().isEmpty()||locationTxt.getText().isEmpty()||textDescription.getText().isEmpty()){
					JOptionPane.showMessageDialog(null,"Please Enter All Fields!");  
				}
				else
					createWorkshop();
			}
		});
		btnSubmit.setBounds(104, 500, 106, 23);
		add(btnSubmit);
		
		JLabel lblStatus = new JLabel("Status:\r\n");
		lblStatus.setBounds(29, 428, 86, 14);
		add(lblStatus);
		
		JButton btnbrowse = new JButton("Choose Image\r\n");
		btnbrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			ImageIcon image=c.displayimageChooser();
			workshopimagelbl.setIcon(image);
			
			}
		});
		btnbrowse.setBounds(343, 500, 120, 23);
		add(btnbrowse);
		
		
		textDescription.setBounds(180, 238, 283, 53);
		add(textDescription);
		
		
		pplcapacity.setModel(new SpinnerNumberModel(10, 10, 300, 1));
		pplcapacity.setBounds(180, 350, 162, 20);
		add(pplcapacity);
		
		
		availableslot.setModel(new SpinnerNumberModel(0, null, 300, 1));
		availableslot.setBounds(180, 387, 162, 20);
		add(availableslot);
		
		
		status.setModel(new DefaultComboBoxModel(new String[] {"ava", "nva"}));
		status.setBounds(180, 425, 129, 20);
		add(status);
		
		
		workshopimagelbl.setBounds(352, 339, 111, 150);
		add(workshopimagelbl);
		
		dstxt = new JTextField();
		dstxt.setBounds(180, 147, 283, 20);
		add(dstxt);
		dstxt.setColumns(10);
		
		detxt = new JTextField();
		detxt.setBounds(180, 201, 283, 20);
		add(detxt);
		detxt.setColumns(10);
		
		JLabel lblTrainerNric = new JLabel("Trainer NRIC:");
		lblTrainerNric.setBounds(29, 472, 72, 14);
		add(lblTrainerNric);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workshopIdTxt.setText("");
				titleTxt.setText("");
				locationTxt.setText("");
				textDescription.setText("");
				dstxt.setText("");
				detxt.setText("");
				pplcapacity.setValue(0);
				availableslot.setValue(0);
				status.removeAllItems();
				
			}
		});
		btnRefresh.setBounds(236, 500, 89, 23);
		add(btnRefresh);
		
		
		trainerBox.setBounds(180, 469, 106, 20);
		for(int i=0; i< Trainer.getMultiple().size();i++){
		trainerBox.addItem(Trainer.getMultiple().get(i).getNric());
		add(trainerBox);
		}
		

	}
	public String getStatus() {
		return (String) status.getSelectedItem();
	}
	public void setStatus(String statusb){
		status.setSelectedItem(statusb);
	}
	public void setWorkshopId(int workshopid){
		workshopIdTxt.setText(Integer.toString(workshopid));
	}
	
	public int getWorkshopId() {
		return Integer.parseInt(workshopIdTxt.getText());
	}
	
	
	public String getTitle(){
		return titleTxt.getText();
	}
	public void setTitle(String title){
		titleTxt.setText(title);
	}
	public String getlocation() {
		return locationTxt.getText();
	}
	public void setlocation(String location){
		locationTxt.setText(location);
	}
	public int getCapacity(){
		return (int) pplcapacity.getValue();
	}
	public void setCapacity(int totalcapacity){
		pplcapacity.setValue(totalcapacity);
	}
	public int getAvailableSlot(){
		return (int) availableslot.getValue();
	}
	public void setAvailableSlot(int slot){
		availableslot.setValue(slot);
	}
	
	public String getDescription(){
		return textDescription.getText();
	}
	public void setDescription(String descriptionarea){
		textDescription.setText(descriptionarea);
	}
	
	public String getNRIC(){
		return (String) trainerBox.getSelectedItem();
	}
	public void setNRIC(String trainernric){
		trainerBox.setSelectedItem(trainernric);
	}
	public void setStartDate(Date dateStart){
		 DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		 String reportDate = df.format(dateStart);
		 dstxt.setText(reportDate);
	}
	
	
	public Date getStartDate(){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		Date date = null;
		try {
		  date=	(Date)formatter.parse(dstxt.getText());
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		System.out.println(date);
		 return date;
		
		
		
		
	}
		public Date getEndDate(){
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
			Date date=null;
			try {
				date = (Date)formatter.parse(detxt.getText());
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			System.out.println(date);
			 return date;
		
		}
		public void setEndDate(Date dateEnd){
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
			 String reportDate = df.format(dateEnd);
			 detxt.setText(reportDate);
		}
	
	public void createWorkshop(){
		WorkshopController con=new WorkshopController(this);
	
		con.CreateWorkshop();
		con.saveImages();
	}
}